package com.sgcc.pda;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.text.TextUtils;

import com.sgcc.pda.common.aidl.CommonResults;

/**
 * @author superFan
 * date: 2019/8/8/008 14:36
 * Copyright (c) 2019 hezhongweiqi.Co.Ltd. All rights reserved.
 * description: commonResults的工厂类
 */
public class CommonResultsFactory {
    /**
     * 当没有返回值时，提示H5 从回调中获取
     *
     * @return
     */
    public static CommonResults noReturnResults() {
        return new CommonResults(CommonResultsConstant.ERROR_CUSTOM.getCode(), "此处没有返回数据，请从回调获取");
    }

    /**
     * 服务连接已断开
     *
     * @return
     */
    public static CommonResults serviceExceptionResults() {
        return new CommonResults(CommonResultsConstant.ERROR_SERVICE_EXCEPTION.getCode(),
                CommonResultsConstant.ERROR_SERVICE_EXCEPTION.getMsg());
    }

    /**
     * aidl远程异常的返回结果
     *
     * @return
     */
    public static CommonResults remoteExceptionResults() {
        return new CommonResults(CommonResultsConstant.ERROR_REMOTE_EXCEPTION.getCode(),
                CommonResultsConstant.ERROR_REMOTE_EXCEPTION.getMsg());
    }

    /**
     * 参数解析异常的返回结果
     *
     * @return
     */
    public static CommonResults parametersExceptionResults() {
        return new CommonResults(CommonResultsConstant.ERROR_PARAMETERS_EXCEPTION.getCode(),
                CommonResultsConstant.ERROR_PARAMETERS_EXCEPTION.getMsg());
    }

    /**
     * 方法反射异常的返回结果
     *
     * @return
     */
    public static CommonResults methodInvokeExceptionResults() {
        return new CommonResults(CommonResultsConstant.ERROR_METHOD_INVOKE_EXCEPTION.getCode(),
                CommonResultsConstant.ERROR_METHOD_INVOKE_EXCEPTION.getMsg());
    }

    /**
     * 类找不到的返回结果
     *
     * @return
     */
    public static CommonResults classNotFoundResults() {
        return classNotFoundResults(null);
    }

    public static CommonResults classNotFoundResults(String className) {
        CommonResults commonResults = new CommonResults(CommonResultsConstant.ERROR_CLASS_NOT_FOUND.getCode(), CommonResultsConstant.ERROR_CLASS_NOT_FOUND.getMsg());
        if (TextUtils.isEmpty(className)) {
            commonResults.setMsg("类名为空！");
        } else {
            commonResults.setMsg(CommonResultsConstant.ERROR_CLASS_NOT_FOUND.getMsg() + ":" + className);
        }
        return commonResults;
    }

    /**
     * 自定义错误信息 的返回结果
     *
     * @param customMsg
     * @return
     */
    public static CommonResults customErrorResults(String customMsg) {
        return new CommonResults(CommonResultsConstant.ERROR_CUSTOM.getCode(),
                TextUtils.isEmpty(customMsg) ? CommonResultsConstant.ERROR_CUSTOM.getMsg() : customMsg);
    }

    /**
     * 成功的结果
     *
     * @return
     */
    public static CommonResults successResults() {
        return new CommonResults(CommonResultsConstant.SUCCESS.getCode(),
                CommonResultsConstant.SUCCESS.getMsg());
    }

    public static CommonResults dbInvokeResult(CommonResults commonResults) {
        commonResults.setCode(CommonResultsConstant.ERROR_DB_INVOKE_EXCEPTION.getCode());
        commonResults.setMsg(CommonResultsConstant.ERROR_DB_INVOKE_EXCEPTION.getMsg());
        return commonResults;
    }


    /**
     * 蓝牙连接状态
     *
     * @param device
     * @param state  1:连接成功；2:连接失败；3：断开连接
     */
    @SuppressLint("MissingPermission")
    public static CommonResults bluetoothConnectListener(BluetoothDevice device, String state) {
        CommonResults commonResults = CommonResultsFactory.successResults();
        commonResults.addResult("bluetoothAddr", device.getAddress());
        commonResults.addResult("state", state);
        commonResults.addResult("bluetoothName", device.getName());
        return commonResults;
    }
}
